#include "main.h"

static LVITEM lvI = { 0 };
static LVITEM lvI_hash = { 0 };
static HWND handle;

extern void listview_winapi_wrapper_init(void)
{
	lvI.mask = lvI_hash.mask = LVIF_TEXT;
	lvI_hash.iSubItem = USERNAME;
	lvI_hash.cchTextMax = 80;
	handle = listview.handle;
}

static void listview_insert_init(void)
{
	lvI.iItem = -1;
	ListView_DeleteAllItems(handle);
}

static void listview_insert_first_column(char *text)
{
	++lvI.iItem;
	lvI.iSubItem = 0;
	lvI.pszText = text;
	assert_notify(ListView_InsertItem(handle, &lvI) != -1);
	++lvI.iSubItem;
}

static void listview_insert(char *text)
{
	lvI.pszText = text;
	assert_notify(ListView_SetItem(handle, &lvI) != -1);
	++lvI.iSubItem;
}

extern void listview_set_username(unsigned hash_val, char *username)
{
	int *i = danmu.index;
	unsigned row = -1;
	lvI_hash.pszText = username;
	
	for (; i != danmu.index_end; ++i)
	{
		if (*i < 0)
			continue;
			
		++row;
		
		if (danmu.head[*i].hash_val != hash_val)
			continue;
		
		lvI_hash.iItem = row;
		assert_notify(ListView_SetItem(handle, &lvI_hash) != -1);
	}
}

extern void insert_danmu(void)
{
	int *i = danmu.index;
	listview_insert_init();

	for (; i != danmu.index_end; ++i)
	{
		char **d, **end;
		
		if (*i < 0)
			continue;
		
		d = danmu.head[*i].dat;
		end = d + DANMU_RECORD_COUNT;
		
		listview_insert_first_column(*d);
		
		for (++d; d != end; ++d)
			listview_insert(*d);
	}
}
